/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.collections;

import name.kudryashov.base.Argument;
import name.kudryashov.functions.Predicate;

import java.util.Collection;
import java.util.Iterator;

import static name.kudryashov.base.Argument.requireNotNull;

class FilteringCollection<E> implements Collection<E> {
    private final Collection<E> c;
    private final Predicate<? super E> predicate;

    public FilteringCollection(Collection<E> c, Predicate<? super E> predicate) {
        this.c = Argument.requireNotNull(c, "c");
        this.predicate = Argument.requireNotNull(predicate, "predicate");
    }

    public int size() {
        return Iterators.size(iterator());
    }

    public boolean isEmpty() {
        return !ExtCollections.exists(c, predicate);
    }

    public boolean contains(Object o) {
        try {
            @SuppressWarnings({"unchecked"})
            E e = (E) o;
            return predicate.apply(e) && c.contains(e);
        }
        catch(ClassCastException e) {
            return false;
        }
    }

    public Iterator<E> iterator() {
        return Iterators.filter(c.iterator(), predicate);
    }

    public Object[] toArray() {
        return Lists.newArrayList(iterator()).toArray();
    }

    @SuppressWarnings({"SuspiciousToArrayCall"})
    public <T> T[] toArray(T[] a) {
        return Lists.newArrayList(iterator()).toArray(a);
    }

    public boolean add(E e) {
        checkElement(e);
        return c.add(e);
    }

    private void checkElement(E e) {
        if(!predicate.apply(e)) {
            throw new IllegalArgumentException(e + " does not match predicate " + predicate);
        }
    }

    public boolean remove(Object o) {
        try {
            @SuppressWarnings({"unchecked"})
            E e = (E) o;
            return predicate.apply(e) && c.remove(e);
        }
        catch(ClassCastException e) {
            return false;
        }
    }

    public boolean containsAll(Collection<?> c) {
        for(Object element : c) {
            if(!contains(element)) {
                return false;
            }
        }
        return true;
    }

    public boolean addAll(Collection<? extends E> c) {
        for(E e : c) {
            checkElement(e);
        }
        return this.c.addAll(c);
    }

    @SuppressWarnings({"SuspiciousMethodCalls"})
    public boolean removeAll(Collection<?> c) {
        requireNotNull(c);
        boolean changed = false;
        Iterator<E> iterator = this.c.iterator();
        while (iterator.hasNext()) {
            E element = iterator.next();
            if (predicate.apply(element) && c.contains(element)) {
                iterator.remove();
                changed = true;
            }
        }
        return changed;
    }

    @SuppressWarnings({"SuspiciousMethodCalls"})
    public boolean retainAll(Collection<?> c) {
        requireNotNull(c);
        boolean changed = false;
        Iterator<E> iterator = this.c.iterator();
        while (iterator.hasNext()) {
            E element = iterator.next();
            if (predicate.apply(element) && !c.contains(element)) {
                iterator.remove();
                changed = true;
            }
        }
        return changed;

    }

    public void clear() {
        Iterator<E> iterator = c.iterator();
        while (iterator.hasNext()) {
            E element = iterator.next();
            if (predicate.apply(element)) {
                iterator.remove();
            }
        }
    }
}